import json
import time
def getname():      #获取白名单用户列表
    global username
    with open('userdata.json', 'r+', encoding='utf-8') as file2:
        username = json.load(file2)
    print(username)

def getheiname():
    global heiname
    with open('hei.json', 'r+', encoding='utf-8') as file2:
        heiname = json.load(file2)

def zhucename():        #注册用户名
    global newname
    newname = input('请输入用户名')
    if newname:
        getheiname()
        for i in heiname:
            if newname==i[0]:
                print('用户名已存在,请重新输入')
                zhucename()
        getname()
        for i in username:
            if i[0] == newname:
                print('用户名已存在,请重新输入')
                zhucename()

        zhecepsw()
    if newname=='':
        print('用户名不能为空,请重新输入')
        zhucename()

def zhecepsw():      #设置密码
    global username

    password = input('请设置密码')
    if password:
        newpassword = input('请确认密码')
        if password != newpassword:
            print('密码不一致,请重新设置密码')
            zhecepsw()
        newuser = [newname, password]
        username.append(newuser)
        with open('userdata.json', 'w', encoding='utf-8') as file1:
            json.dump(username, file1)
        print(username)
        print('注册成功,正在跳转至登陆页面')
        time.sleep(2)
        shouye()

    else:
        print('密码不能为空,请重新输入')
        zhecepsw()
def confirmpassword():   #确认密码
    global b
    loginpsw = input('请输入密码')
    if loginpsw == password_1:
        print('登陆成功,正在跳转至主页面')
        pass
    else:
        b+=1
        if b==4:
            print('密码错误次数过多,该账号已冻结')
            frozenuser()
            shouye()
        if b<4:
            print('密码输入错误,请重新输入')
            confirmpassword()

def login():         #登陆程序
    getname()
    a=0
    global password_1
    global b
    global loginname
    loginname=input('请输入用户名')
    getheiname()
    for i in heiname:
        if i[0]==loginname:
            print('此用户名已被冻结,请更换用户名')
            login()
    for i in username:
        if i[0]==loginname:
            password_1=i[1]
            a=1
            b=1
            confirmpassword()
    if a==0:
        print('用户名输入错误,请重新输入')
        login()

def frozenuser():      #冻结账户
    getname()
    getheiname()
    for i in username:
        if i[0]==loginname:
            heiname.append(i)
            with open('hei.json','w+',encoding='utf-8') as file1:
                json.dump(heiname,file1)
            username.remove(i)
            with open('userdata.json','w+',encoding='utf-8') as file1:
                json.dump(username,file1)

def shouye():
    print('欢迎来到jd商城')
    print('   请选择')
    choice=input('1,登陆 2,注册')
    if choice=='1':
        login()
    if choice=='2':
        zhucename()

shouye()
# getname()
# # getheiname()